// pages/preview/preDetail.js
const App = getApp();
const navigationBarHeight = (App.statusBarHeight + 44) + 'px'
var config = require('../../utils/config');
var net = require('../../utils/net.js');
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navigationBarTitle: '予約変更',
    navigationBarHeight,

    noTedian:true,
    model:{},
    detailModel:{},
    showCancle:false,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    new App.ToastPannel();


    this.orderId = options.orderId

    if (App.globalData.isLogin) {
      this.requestDetailData();

    } else {
      App.loginCallback = res => {
        this.requestDetailData();
      }
    } 
  },

  requestDetailData:function(){
    var url = config.service.orderDetailUrl
    var params = {
      id: this.orderId
    }
    net.doRequest(url, params, 'GET', true, net.Token, res => {
      if (res.code == 0) {
        var mo = res.data
        mo.orderTime = util.zhuanHuaTime(mo.orderTime,'h:m')
        var x = mo.orderDate
        mo.orderDateStr = util.zhuanHuaTime(x,'Y年M月D日')
        mo.orderDate = util.zhuanHuaTime(x, 'Y-M-D')
        var timeString = ''
        if (!util.isNull(mo.wMerchantDo.operatedBegin1)){
          mo.wMerchantDo.operatedBegin1 = util.zhuanHuaTime(mo.wMerchantDo.operatedBegin1, 'h:m')
          mo.wMerchantDo.operatedEnd1 = util.zhuanHuaTime(mo.wMerchantDo.operatedEnd1, 'h:m')
          timeString = mo.wMerchantDo.operatedBegin1 + '-' + mo.wMerchantDo.operatedEnd1
        }
        if (!util.isNull(mo.wMerchantDo.operatedBegin2)){
          mo.wMerchantDo.operatedBegin2 = util.zhuanHuaTime(mo.wMerchantDo.operatedBegin2, 'h:m')
          mo.wMerchantDo.operatedEnd2 = util.zhuanHuaTime(mo.wMerchantDo.operatedEnd2, 'h:m')
          timeString = timeString + ' ' + mo.wMerchantDo.operatedBegin2 + '-' + mo.wMerchantDo.operatedEnd2
        }
        if (!util.isNull(mo.wMerchantDo.operatedBegin3)) {
          mo.wMerchantDo.operatedBegin3 = util.zhuanHuaTime(mo.wMerchantDo.operatedBegin3, 'h:m')
          mo.wMerchantDo.operatedEnd3 = util.zhuanHuaTime(mo.wMerchantDo.operatedEnd3, 'h:m')
          timeString = timeString + ' ' + mo.wMerchantDo.operatedBegin3 + '-' + mo.wMerchantDo.operatedEnd3
        }
        mo.wMerchantDo.timeString = timeString;
       
        mo.wMerchantDo.shopImg1 = config.service.picUrl + mo.wMerchantDo.shopImg1
        
        this.setData({
          detailModel: mo,
          state:mo.state
        })
        if (!util.isBlank(mo.wdIscountDtoOut)){
          this.setData({
            noTedian:false
          })
          var item = mo.wdIscountDtoOut
          if (!util.isNull(item.limitedAt)) {
            item.limitedAtStr = util.zhuanHuaTime(item.limitedAt, 'Y年M月D日')
          }
          this.setData({
            detailModel: mo,
          })
        }
      } else {
        this.show(res.message);
      }
    })

  },


  //左按钮点击
  cancleClick:function(){
    this.setData({
      showCancle:true
    })

  },


  //右边按钮点击
  yudingClick: function () {
    
    wx.navigateTo({
      url: '../index/sureYuding?orderId=' + this.data.detailModel.id + '&shopId=' + this.data.detailModel.wMerchantDo.id +'&from=1',
    })
  },

  modeTap:function(){
    this.setData({
      showCancle: false
    })
  },

  cancleViewCancleClick:function(){
    this.setData({
      showCancle: false
    })
  },

 //取消订单
  cancleViewSureClick:function(){
    this.setData({
      showCancle: false
    })
  
    var url = config.service.host + '/wechat/orders/'+ this.orderId+'/cancel.do';

    var params = {
      id: this.data.model.orderId
    }
    net.doRequest(url, params, 'PUT', true, net.Token, res => {
      if (res.code == 0) {
        this.show('订单取消成功');
        wx.navigateBack();
       
      } else {
        this.show(res.message);
      }
    })
    
  },

  //去店铺详情

  toShopDetail:function(){
    wx.navigateTo({
      url: '../index/indexDetail?shopId=' + this.data.detailModel.wMerchantDo.id + '&shopName=' + this.data.detailModel.wMerchantDo.shopName
    })
  }



})